package com.yimi.yinhepda.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.yimi.yinhepda.R;
import com.yimi.yinhepda.entity.response.CheckScanTaskResponse;
import com.yimi.yinhepda.utils.Common;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 000432
 * @date 2017/4/13 0013
 * <p>
 * 盘点扫描列表的适配器
 */

public class CheckScanListAdapter extends BaseAdapter {

    private Context mContext;
    private List<CheckScanTaskResponse.CheckScanEntity> mDataList = new ArrayList<>();

    public CheckScanListAdapter(Context context, List<CheckScanTaskResponse.CheckScanEntity> mDataList) {
        this.mContext = context;
        this.mDataList = mDataList;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.load_scan_item2, null);
            viewHolder.waybillNo = convertView.findViewById(R.id.text1);
            viewHolder.tvProductTye = convertView.findViewById(R.id.tv_produce_type);
            viewHolder.packingSpe = convertView.findViewById(R.id.text2);
            viewHolder.count = convertView.findViewById(R.id.text3);
            viewHolder.stock = convertView.findViewById(R.id.text4);
            viewHolder.scaned = convertView.findViewById(R.id.text5);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        CheckScanTaskResponse.CheckScanEntity checkScanEntity = mDataList.get(position);
        //运单号
        viewHolder.waybillNo.setText(checkScanEntity.getWaybillNo());
        //包装
        viewHolder.packingSpe.setText(checkScanEntity.getPackingSpecification());
        //开单数量
        viewHolder.count.setText(checkScanEntity.getQuantity() + "");
        //库存数量
        viewHolder.stock.setText(checkScanEntity.getInventoryNum() + "");
        //已扫描数量
        viewHolder.scaned.setText(checkScanEntity.getScannedNum() + "");

        //二级排序：获取产品类型
        int secondSort = checkScanEntity.getSecondSort();
        viewHolder.tvProductTye.setText(Common.getSecondType(secondSort));

        //处理颜色
        int color = checkScanEntity.justColorShow();
        switch (color) {
            case 0:
                convertView.setBackgroundColor(mContext.getResources().getColor(R.color.red));
                break;
            case 1:
                convertView.setBackgroundColor(mContext.getResources().getColor(R.color.white));
                break;
            case 2:
                convertView.setBackgroundColor(mContext.getResources().getColor(R.color.yellow));
                break;
            case 3:
                convertView.setBackgroundColor(mContext.getResources().getColor(R.color.green));
                break;
            default:
                break;
        }

        return convertView;
    }

    public static class ViewHolder {
        /**
         * 运单号
         */
        TextView waybillNo;
        /**
         * 产品类型
         */
        TextView tvProductTye;
        /**
         * 包装
         */
        TextView packingSpe;
        /**
         * 开单
         */
        TextView count;
        /**
         * 库存
         */
        TextView stock;
        /**
         * 扫描
         */
        TextView scaned;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Object getItem(int i) {
        return mDataList.get(i);
    }

    @Override
    public int getCount() {
        return mDataList == null ? 0 : mDataList.size();
    }

    public void setmDataList(List<CheckScanTaskResponse.CheckScanEntity> mDataList) {
        this.mDataList = mDataList;
    }
}
